package com.ruoyi.system.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.DataAndPage;
import com.ruoyi.system.domain.SysPythonEntity;
import com.ruoyi.system.domain.SysPythonUrlEntity;
import com.ruoyi.system.mapper.ISysPythonMapper;
import com.ruoyi.system.mapper.ISysPythonUrlMapper;
import com.ruoyi.system.util.UrlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 爬虫自动
 */
@Component
public class ApiAutoTask {
    @Autowired
    private ISysPythonUrlMapper iSysPythonUrlMapper;
    @Autowired
    private ISysPythonMapper iSysPythonMapper;
    @Autowired
    private RedisCache redisCache;
    @Scheduled(cron = "0/3 * * * * ?")
    public void autoApiRun(){
        List<SysPythonUrlEntity> sysPythonUrlEntities = iSysPythonUrlMapper.selectList(new LambdaQueryWrapper<>());
        sysPythonUrlEntities.forEach(sysPythonUrlEntity -> {
            if (sysPythonUrlEntity.getStatus() ==0) {
                autoApi(sysPythonUrlEntity);
            }
        });
    }

    /**
     * 暂时只支持bilibili爬虫
     * @param sysPythonUrlEntity
     */
    @Async("handleMsgExecutor")
    public void autoApi(SysPythonUrlEntity sysPythonUrlEntity){
        String url = sysPythonUrlEntity.getUrl();
        String userAgent = sysPythonUrlEntity.getUserAgent();
        String referer = sysPythonUrlEntity.getReferer();
        String cookie = sysPythonUrlEntity.getCookie();
        String origin = sysPythonUrlEntity.getOrigin();
        if (StringUtils.isNotEmpty(url)&&StringUtils.isNotEmpty(userAgent)&&StringUtils.isNotEmpty(referer)&&StringUtils.isNotEmpty(cookie)&&StringUtils.isNotEmpty(origin)) {
            String newUrl = url.concat("&&page=" + redisCache.getCacheObject(sysPythonUrlEntity.getName()));

            DataAndPage dataAndPage = UrlUtil.crawlRecommendationRoomsSelect(newUrl, userAgent, referer, cookie, origin);
            if (dataAndPage == null) {

            } else {
                List<Map<String, Object>> dataList = dataAndPage.getData();
                //为null时拦截并递归
                if (dataList == null) {
                    //为null时说明page超出范围
                    redisCache.setCacheObject(sysPythonUrlEntity.getName(), 1);
                    autoApi(sysPythonUrlEntity);
                    return;
                }
                dataList.forEach(data -> {
                    if (data !=null) {
                        SysPythonEntity sysPythonEntity = new SysPythonEntity();
                        sysPythonEntity.setTName(data.get("author").toString());
                        sysPythonEntity.setAddress("1");
                        sysPythonEntity.setSubject(data.get("typename").toString());
                        sysPythonEntity.setTitle(removeHtmlTags(data.get("title").toString()));
                        sysPythonEntity.setValue(data.get("description").toString());
                        sysPythonEntity.setNum(Integer.valueOf(data.get("play").toString()));
                        sysPythonEntity.setDuration(Integer.valueOf(data.get("danmaku").toString()));
                        sysPythonEntity.setUrl(data.get("arcurl").toString());
                        sysPythonEntity.setUpdateTime(LocalDateTime.now());
                        sysPythonEntity.setCreatTime(LocalDateTime.now());
                        Integer page = redisCache.getCacheObject(sysPythonUrlEntity.getName());
                        redisCache.setCacheObject(sysPythonUrlEntity.getName(), ++page);
                        iSysPythonMapper.insertData(sysPythonEntity);
                    }
                });
            }
        }
    }

    private static String removeHtmlTags(String input) {
        // 移除HTML标签
        Pattern patternHtml = Pattern.compile("<[^>]*>");
        Matcher matcherHtml = patternHtml.matcher(input);
        String resultHtml = matcherHtml.replaceAll("");

        // 移除单引号和双引号
        Pattern patternQuotes = Pattern.compile("[\"']");
        Matcher matcherQuotes = patternQuotes.matcher(resultHtml);
        String resultQuotes = matcherQuotes.replaceAll("");

        return resultQuotes;
    }

}
